package com.anysoft.me.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.anysoft.base.BaseActivity;
import com.anysoft.utils.MentorLogger;
import com.anysoft.view.dialog.LoadingDialog;
import com.zhy.autolayout.AutoLayoutActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lty on 2017/12/3.
 */

public abstract class BaseBaseActivity extends AutoLayoutActivity {

    private static final String TAG = "test";
    private LoadingDialog loadingDialog;
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        doBeforeSetContentView();
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        try {
            EventBus.getDefault().register(this);
        } catch (Exception e) {
            MentorLogger.Loge(TAG, "已订阅");
        }
        initView();
        registLinstener();
        loadDataAndUpdateUI();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    /**
     * 在布局之前做的一些操作
     */
    protected void doBeforeSetContentView() {

    }

    /**
     * 实现类管理布局Id
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 加载数据并更新ui
     */
    protected abstract void loadDataAndUpdateUI();

    /**
     * 注册监听器
     */
    protected abstract void registLinstener();

    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 展示加载提示框
     * @param context
     */
    public void showLoading(Context context) {
        if (loadingDialog == null && !((BaseActivity) context).isFinishing()) {
            loadingDialog = new LoadingDialog(context);
            loadingDialog.setCancelable(false);
            loadingDialog.setAnimationEnable(true);
            loadingDialog.show();
        }
    }

    /**
     * 隐藏加载提示框
     */
    public void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        hideLoading();
        unbinder.unbind();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }
}
